package com.xw.bo.ks.config;

import com.xw.bo.ks.service.WebSocketUserService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.WebSocketListener;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@Slf4j
@Component
@ServerEndpoint(value = "/websocket/{friendId}/{accesskey}", configurator = WebSocketConfig.class)
public class WebSocketEndpoint extends WebSocketListener {

    private WebSocketUserService webSocketUserService = SpringUtil.getBean(WebSocketUserService.class);


    @OnOpen
    public void onOpen(@PathParam("friendId") String friendId, @PathParam("accesskey") String accesskey, Session session) {
        // 连接打开时的逻辑
        webSocketUserService.onOpen(friendId, accesskey, session);
    }

    @OnClose
    public void onClose(Session session) {
        // 连接关闭时的逻辑
        webSocketUserService.onClose(session);

    }

    @OnMessage
    public void onMessage(String message, Session session) {
        // 接收到消息时的逻辑
        webSocketUserService.onMessage(message, session);

    }

}
